package net.sunofbeach.blog.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sunofbeach.blog.pojo.Looper;
import net.sunofbeach.blog.pojo.User;
import net.sunofbeach.blog.pojo.query.LoopQuery;
import net.sunofbeach.blog.response.Result;
import net.sunofbeach.blog.response.ResultEnum;
import net.sunofbeach.blog.service.LooperService;
import net.sunofbeach.blog.service.UserService;
import net.sunofbeach.blog.utils.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/admin/loop")
public class LooperAdminApi {

    @Resource
    private LooperService looperService;

    @Resource
    private UserService userService;

    /**
     * 添加图片
     *
     * @return
     */
    @ApiOperation("新增轮播图")
    @PostMapping
    public Result addLoop(@RequestBody Looper looper) {
        // 检查数据
        if (StringUtils.isEmpty(looper.getTitle())) {
            return Result.fail().message("标题不能为空");
        }

        if (StringUtils.isEmpty(looper.getImageUrl())) {
            return Result.fail().message("图片地址不能为空");
        }
        if (StringUtils.isEmpty(looper.getTargetUrl())) {
            return Result.fail().message("跳转链接地址不能为空");
        }
        boolean result = looperService.save(looper);
        if (result) {
            return Result.ok().message("添加轮播图成功");
        }
        return Result.fail().message("添加轮播图失败");
    }


    @ApiOperation("根据ID查询轮播图")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/{loopId}")
    public Result getLoop(@PathVariable("loopId") String loopId) {
        if (StringUtils.isEmpty(loopId)) {
            return Result.fail().message("轮播图ID不能为空");
        }
        Looper looperFromDb = looperService.getById(loopId);
        if (looperFromDb == null) {
            return Result.fail().message("该轮播图不存在");
        }
        return Result.ok(looperFromDb).message("轮播图获取成功");
    }


    /**
     * 条件分页查询轮播图
     *
     * @return
     */
    @ApiOperation("条件分页查询轮播图")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/list/{page}/{size}")
    public Result listLoops(@PathVariable("page") int page, @PathVariable("size") int size, LoopQuery query) {
        Page<Looper> pageParam = new Page<>(page, size);
        IPage<Looper> pageModel = looperService.selectPage(pageParam, query);
        return Result.ok(pageModel);
    }


    /**
     * 编辑
     *
     * @param looper
     * @return
     */
    @ApiOperation("更新轮播图")
    @PutMapping
    public Result updateLoop(@RequestBody Looper looper) {
        // 检验当前操作用户的身份
        User currentUser = userService.checkSobUser();
        if (currentUser == null) {
            // 账号未登录
            return Result.build(null, ResultEnum.ACCOUNT_NOT_LOGIN);
        }
        // 判断角色，拥有 role_admin 角色可以操作
        if (!Constants.User.ROLE_ADMIN.equals(currentUser.getRole())) {
            // 权限不足
            return Result.build(null, ResultEnum.PERMISSION_DENIED);
        }

        if (StringUtils.isEmpty(looper.getTargetUrl())) {
            return Result.fail().message("跳转链接地址不能为空");
        }
        if (StringUtils.isEmpty(looper.getImageUrl())) {
            return Result.fail().message("轮播图片地址不能为空");
        }

        if (StringUtils.isEmpty(looper.getTitle())) {
            return Result.fail().message("轮播图标题不能为空");
        }
        boolean result = looperService.updateById(looper);
        if (result) {
            return Result.ok().message("更新轮播图成功");
        }
        return Result.fail().message("更新轮播图失败");
    }


    /**
     * 逻辑删除
     * 更新状态值为 0 表示已删除
     * @param loopId
     * @return
     */
    @ApiOperation("根据ID删除轮播图")
    @DeleteMapping("/{loopId}")
    public Result deleteLoop(@PathVariable("loopId") String loopId) {

        // 检验当前操作用户的身份
        User currentUser = userService.checkSobUser();
        if (currentUser == null) {
            // 账号未登录
            return Result.build(null, ResultEnum.ACCOUNT_NOT_LOGIN);
        }
        // 判断角色，拥有 role_admin 角色可以操作
        if (!Constants.User.ROLE_ADMIN.equals(currentUser.getRole())) {
            // 权限不足
            return Result.build(null, ResultEnum.PERMISSION_DENIED);
        }
        // 判断数据
        if (StringUtils.isEmpty(loopId)) {
            return Result.fail().message("轮播图ID不能为空");
        }
        // 查出来
        Looper looperFromDb = looperService.getById(loopId);
        if (looperFromDb == null) {
            return Result.fail().message("该轮播图不存在");
        }
        // 更新状态为 0 表示删除
        looperFromDb.setState(Constants.Status.DELETE);
        // 执行更新
        boolean result = looperService.updateById(looperFromDb);
        // 返回结果
        if (result) {
            return Result.ok().message("删除轮播图成功");
        }
        return Result.fail().message("删除轮播图失败");
    }



}

